"""
Case Type   : 系统视图
Case Name   : 测试系统视图information_schema._PG_FOREIGN_DATA_WRAPPERS字段与数据类型是否正确
Create At   : 2024/07/13
Owner       : sungang14
Description : 
    1.查看系统视图information_schema._PG_FOREIGN_DATA_WRAPPERS的结构
    \d information_schema._PG_FOREIGN_DATA_WRAPPERS
    2.该表字段与对应字段数据类型是否如下：
     oid                           | oid
     fdwowner                      | oid
     fdwoptions                    | text[]
     foreign_data_wrapper_catalog  | information_schema.sql_identifier
     foreign_data_wrapper_name     | information_schema.sql_identifier
     authorization_identifier      | information_schema.sql_identifier
     foreign_data_wrapper_language | information_schema.character_data
    3.验证视图只有sysadmin权限可以查看。
    3.1创建普通用户"create user u_systemtable_0300_1 password 'Huawei@123'"
    使用u_systemtable_0300_1用户访问系统表"select * from information_schema._PG_FOREIGN_DATA_WRAPPERS;"
    3.2创建sysadmin用户"create user u_systemtable_0300_2 sysadmin password 'Huawei@123'"
    使用u_systemtable_0300_2用户访问系统表"select * from information_schema._PG_FOREIGN_DATA_WRAPPERS;"
    4.验证视图显示外部数据封装器的信息
    select foreign_data_wrapper_name from information_schema._PG_FOREIGN_DATA_WRAPPERS;
Expect      : 
    1.查看系统表information_schema._PG_FOREIGN_DATA_WRAPPERS表结构成功
    2.该表字段与字段数据类型对应正确
    3.1.无权限查看
    3.2.回显正确
    4、 foreign_data_wrapper_name
        ---------------------------
         mot_fdw
         log_fdw
         file_fdw
         dist_fdw
History     : 
"""

import sys
import os
import unittest
from yat.test import Node
from yat.test import macro

sys.path.append(sys.path[0] + "/../")
from testcase.utils.Logger import Logger
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH


class IndexFileDamaged(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.logger.info(f'-----{os.path.basename(__file__)} start-----')
        self.logger.info('----------------this is setup-----------------------')
        self.userNode = Node('dbuser')
        self.DB_ENV_PATH = macro.DB_ENV_PATH
        self.DB_INSTANCE_PATH = macro.DB_INSTANCE_PATH
        self.com = Common()
        self.comsh = CommonSH('dbuser')
        self.expect_result_dict = {
            'Column': ['oid', 'fdwowner', 'fdwoptions', 'foreign_data_wrapper_catalog', 'foreign_data_wrapper_name',
                       'authorization_identifier', 'foreign_data_wrapper_language'],
            'Type': ['oid', 'oid', 'text[]', 'information_schema.sql_identifier', 'information_schema.sql_identifier',
                     'information_schema.sql_identifier', 'information_schema.character_data']
        }
        msg = self.comsh.execut_db_sql("drop user if exists u_systemtable_0300_1;")
        self.logger.info(msg)
        msg = self.comsh.execut_db_sql("create user u_systemtable_0300_1 password 'Huawei@123';")
        self.assertEqual("CREATE ROLE", msg)
        msg = self.comsh.execut_db_sql("drop user if exists u_systemtable_0300_2;")
        self.logger.info(msg)
        msg = self.comsh.execut_db_sql("create user u_systemtable_0300_2 sysadmin password 'Huawei@123';")
        self.assertEqual("CREATE ROLE", msg)

    def test_Index_file_damaged(self):
        self.logger.info('--step1.查看系统视图information_schema._PG_FOREIGN_DATA_WRAPPERS的结构')
        msg = self.comsh.execut_db_sql('\d information_schema._PG_FOREIGN_DATA_WRAPPERS')
        self.logger.info(msg)
        result_dict = self.com.format_sql_result(msg)
        self.logger.info(result_dict)
        del result_dict['Modifiers']
        self.assertDictEqual(self.expect_result_dict, result_dict)
        self.logger.info("--step3:验证视图只有sysadmin权限可以查看")
        msg = self.comsh.execut_db_sql("select * from information_schema._PG_FOREIGN_DATA_WRAPPERS;",
                                       sql_type=f" -U u_systemtable_0300_1 -W Huawei@123")
        self.logger.info(msg)
        self.assertEqual("ERROR:  permission denied for relation _pg_foreign_data_wrappers\nDETAIL:  N/A", msg)
        msg = self.comsh.execut_db_sql("select * from information_schema._PG_FOREIGN_DATA_WRAPPERS;",
                                       sql_type=f" -U u_systemtable_0300_2 -W Huawei@123")
        self.logger.info(msg)
        self.assertIn("foreign_data_wrapper_name", msg)
        self.logger.info("--step4:验证视图显示外部数据封装器的信息")
        msg = self.comsh.execut_db_sql(
            "select foreign_data_wrapper_name from information_schema._PG_FOREIGN_DATA_WRAPPERS;")
        self.logger.info(msg)
        self.assertIn("mot_fdw" and "log_fdw" and "file_fdw" and "dist_fdw", msg)

    def tearDown(self):
        self.logger.info('----------------this is tearDown-----------------------')
        # 删除用户
        msg = self.comsh.execut_db_sql("drop user if exists u_systemtable_0300_1;")
        self.logger.info(msg)
        self.assertEqual("DROP ROLE", msg)
        msg = self.comsh.execut_db_sql("drop user if exists u_systemtable_0300_2;")
        self.logger.info(msg)
        self.assertEqual("DROP ROLE", msg)
        self.logger.info(f'-----{os.path.basename(__file__)} end-----')
